using EasyXaf.Sample.ODataService.Models;
using Microsoft.AspNetCore.OData;
using Microsoft.OData.ModelBuilder;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var modelBuilder = new ODataConventionModelBuilder();

modelBuilder.EntityType<Company>().Collection
    .Function("GetDepartmentCount")
    .Returns<int>();

modelBuilder.EntityType<Company>().Collection
    .Action("UpdateCompanyName")
    .Parameter<string>("prefix");

modelBuilder.EntitySet<Company>("Companies");
modelBuilder.EntitySet<Department>("Departments");
modelBuilder.EntitySet<Tianqi>("Tianqi");

builder.Services.AddControllers().AddOData(
    options => options
        .EnableQueryFeatures()
        .AddRouteComponents("odata", modelBuilder.GetEdmModel())
);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Use(async (context, next) =>
{
    Console.WriteLine($"{context.Request.Method}:{context.Request.Path}{context.Request.QueryString}");
    await next();
});

app.Run();
